﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using PPI.Entities.Distribution;
using System.Data;
using PPI.Mobile.Logic;
using VMind.Global;
using PPI.General;

namespace PPI.Client.Logic.Distribution
{
    public class LTransformer
    {
        public static List<ETransformer> Save(List<ETransformer> transformers, Int32 poleId)
        {
            return WebConsumer.Distribution.WPole.Save(poleId, transformers);
        }

        public static List<ETransformer> GetPoleTransformers(EDistributionPole distributionPole, string connectionString)
        {
            StringBuilder _sql = new StringBuilder();

            #region Query

            _sql.AppendLine("SELECT ");
            _sql.AppendLine("   T.TransformerServerId, ");
            _sql.AppendLine("   T.TransformerId, ");
            _sql.AppendLine("   T.Latitude, ");
            _sql.AppendLine("   T.Longitude, ");
            _sql.AppendLine("   T.SerialNumber, ");
            _sql.AppendLine("   T.CreatedDate, ");
            _sql.AppendLine("   T.ImageFileName, ");
            _sql.AppendLine("   T.UploadedImage, ");
            _sql.AppendLine("   T.Comments, ");
            _sql.AppendLine("   T.TransformerType, ");
            _sql.AppendLine("   T.Type, ");
            _sql.AppendLine("   T.Class, ");
            _sql.AppendLine("   T.Phase, ");
            _sql.AppendLine("   T.UtilityNumber, ");
            _sql.AppendLine("   T.Manufacturer, ");
            _sql.AppendLine("   T.[Primary], ");
            _sql.AppendLine("   T.Secondary, ");
            _sql.AppendLine("   T.Arrestor, ");
            _sql.AppendLine("   T.DualRated, ");
            _sql.AppendLine("   T.Pre1978, ");
            _sql.AppendLine("   T.PCBType, ");
            _sql.AppendLine("   T.PPM, ");
            _sql.AppendLine("   T.ArrestorKv, ");
            _sql.AppendLine("   T.CutOut, ");
            _sql.AppendLine("   T.CutOutKv, ");
            _sql.AppendLine("   T.PoleNumber, ");
            _sql.AppendLine("   T.Can, ");
            _sql.AppendLine("   T.DeterminedBy, ");
            _sql.AppendLine("   T.TestNumber, ");
            _sql.AppendLine("   T.Impeddance, ");
            _sql.AppendLine("   T.Taps, ");
            _sql.AppendLine("   T.Polarity, ");
            _sql.AppendLine("   T.ManufDate, ");
            _sql.AppendLine("   T.Sticker ");
            _sql.AppendLine("FROM tb_Transformers T ");
            _sql.AppendLine("WHERE T.PoleFk = " + distributionPole.Id + ";");
            
            #endregion

            DataTable dt = LMobileDatabase.ExecuteQueryDataset(_sql.ToString(), connectionString).Tables[0];

            List<ETransformer> lstTransformers = new List<ETransformer>();

            foreach (DataRow dr in dt.Rows)
            {
                ETransformer poleTransformer = new ETransformer();

                poleTransformer.Id = ConvertHelper.ToInt32(dr["TransformerId"]);
                poleTransformer.ServerId = ConvertHelper.ToInt32(dr["TransformerServerID"]);
                poleTransformer.Latitude = ConvertHelper.Todouble(dr["Latitude"]);
                poleTransformer.Longitude = ConvertHelper.Todouble(dr["Longitude"]);
                poleTransformer.CreatedDate = ConvertHelper.ToDateTime(dr["CreatedDate"]);
                poleTransformer.SerialNumber = dr["SerialNumber"].ToString();
               /* poleTransformer.Image.Id = 0;
                poleTransformer.Image.FileName = dr["ImageFileName"].ToString();

                string _imageDirectory = DirHelper.GetImagesDirectory(connectionString);
                poleTransformer.Image.FullName = _imageDirectory + Path.DirectorySeparatorChar + poleTransformer.Image.FileName;

                poleTransformer.Image.ImageType = DistributionImageType.Transformer;*/
                poleTransformer.Comments = dr["Comments"].ToString();
                poleTransformer.UploadedImage = ConvertHelper.Tobool(dr["UploadedImage"]);

                if (dr["TransformerType"] != DBNull.Value)
                    poleTransformer.TransformerType.Id = ConvertHelper.ToInt32(dr["TransformerType"]);

                if (dr["Class"] != DBNull.Value)
                    poleTransformer.Class.Id = ConvertHelper.ToInt32(dr["Class"]);

                if (dr["Phase"] != DBNull.Value)
                    poleTransformer.Phase.Id = ConvertHelper.ToInt32(dr["Phase"]);

                if (dr["Type"] != DBNull.Value)
                    poleTransformer.Type.Id = ConvertHelper.ToInt32(dr["Type"]);

                if (dr["PCBType"] != DBNull.Value)
                    poleTransformer.PCBType.Id = ConvertHelper.ToInt32(dr["PCBType"]);

                if (dr["UtilityNumber"] != DBNull.Value)
                    poleTransformer.UtilityNumber = ConvertHelper.Tostring(dr["UtilityNumber"]);

                if (dr["Manufacturer"] != DBNull.Value)
                    poleTransformer.Manufacturer = ConvertHelper.Tostring(dr["Manufacturer"]);

                if (dr["Primary"] != DBNull.Value)
                    poleTransformer.Primary = ConvertHelper.Tostring(dr["Primary"]);

                if (dr["Secondary"] != DBNull.Value)
                    poleTransformer.Secondary = ConvertHelper.Tostring(dr["Secondary"]);

                if (dr["Arrestor"] != DBNull.Value)
                    poleTransformer.Arrestor = ConvertHelper.Tobool(dr["Arrestor"]);

                if (dr["DualRated"] != DBNull.Value)
                    poleTransformer.DualRated = ConvertHelper.Tobool(dr["DualRated"]);

                if (dr["Pre1978"] != DBNull.Value)
                    poleTransformer.Pre1978 = ConvertHelper.Tobool(dr["Pre1978"]);

                if (dr["PPM"] != DBNull.Value)
                    poleTransformer.PPM = ConvertHelper.Tostring(dr["PPM"]);

                if (dr["PoleNumber"] != DBNull.Value)
                    poleTransformer.TransformerNumber = ConvertHelper.Tostring(dr["PoleNumber"]);

                if (dr["CutOut"] != DBNull.Value)
                    poleTransformer.CutOut = ConvertHelper.Tobool(dr["CutOut"]);

                if (dr["ArrestorKv"] != DBNull.Value)
                    poleTransformer.ArrestorKv.Id = ConvertHelper.ToInt32(dr["ArrestorKv"]);

                if (dr["CutOutKv"] != DBNull.Value)
                    poleTransformer.CutOutKv.Id = ConvertHelper.ToInt32(dr["CutOutKv"]);

                if (dr["PCBType"] != DBNull.Value)
                    poleTransformer.PCBType.Id = ConvertHelper.ToInt32(dr["PCBType"]);

                if (dr["Can"] != DBNull.Value)
                    poleTransformer.Can.Id = ConvertHelper.ToInt32(dr["Can"]);

                if (dr["DeterminedBy"] != DBNull.Value)
                    poleTransformer.DeterminedBy.Id = ConvertHelper.ToInt32(dr["DeterminedBy"]);

                if (dr["TestNumber"] != DBNull.Value)
                    poleTransformer.TestNumber = ConvertHelper.Tostring(dr["TestNumber"]);

                if (dr["Impeddance"] != DBNull.Value)
                    poleTransformer.Impeddance = ConvertHelper.Tostring(dr["Impeddance"]);

                if (dr["Taps"] != DBNull.Value)
                    poleTransformer.Taps = ConvertHelper.Tostring(dr["Taps"]);

                if (dr["Polarity"] != DBNull.Value)
                    poleTransformer.Polarity = ConvertHelper.Tostring(dr["Polarity"]);

                if (dr["ManufDate"] != DBNull.Value)
                    poleTransformer.ManufDate = ConvertHelper.ToDateTime(dr["ManufDate"]);

                if (dr["Sticker"] != DBNull.Value)
                    poleTransformer.Sticker = ConvertHelper.Tobool(dr["Sticker"]);

                poleTransformer.Images = LImage.GetTransformerImages(poleTransformer.Id, connectionString);

                _sql = new StringBuilder();

                _sql.AppendLine("SELECT ");
                _sql.AppendLine(" idKeyword  ");
                _sql.AppendLine("FROM tb_Transformers_Keywords ");
                _sql.AppendLine("WHERE idTransformer = " + poleTransformer.Id );

                dt = LMobileDatabase.ExecuteQueryDataset(_sql.ToString(), connectionString).Tables[0];
                poleTransformer.Notes = new List<EDistributionLookUp>();
                foreach (DataRow dr2 in dt.Rows)
                {
                    EDistributionLookUp edl = new EDistributionLookUp();

                    if (dr2["idKeyword"] != DBNull.Value)
                        edl.Id = ConvertHelper.ToInt32(dr2["idKeyword"]);

                    poleTransformer.Notes.Add(edl);
                }

                lstTransformers.Add(poleTransformer);
            }

            return lstTransformers;
        }

        public static ETransformer Update(string connectionString, EDistributionPole pole, ETransformer transformer)
        {
            var query = new StringBuilder();
            var listParameters = new List<SqlParameter>();

            try
            {
                #region - Pole UPDATE -

                query.AppendLine("UPDATE tb_Transformers SET ");
                query.AppendLine("Latitude = @Latitude,");
                query.AppendLine("Longitude = @Longitude,");
                query.AppendLine("PoleFk = @PoleFk,");

                query.AppendLine("Comments = @Comments, ");
                query.AppendLine("SerialNumber = @SerialNumber, ");
                query.AppendLine("TransformerType = @TransformerType,");
                query.AppendLine("Type = @Type,");
                query.AppendLine("Class = @Class,");
                query.AppendLine("Phase = @Phase,");
                query.AppendLine("UtilityNumber = @UtilityNumber,");
                query.AppendLine("Manufacturer = @Manufacturer, ");
                query.AppendLine("[Primary] = @Primary,");
                query.AppendLine("Secondary = @Secondary,");
                query.AppendLine("Arrestor = @Arrestor,");
                query.AppendLine("DualRated = @DualRated,");
                query.AppendLine("Pre1978 = @Pre1978,");
                query.AppendLine("CutOut = @CutOut,");
                query.AppendLine("ArrestorKv = @ArrestorKv,");
                query.AppendLine("CutOutKv = @CutOutKv,");
                query.AppendLine("Can = @Can,");
                query.AppendLine("PoleNumber = @PoleNumber,");
                query.AppendLine("PCBType = @PCBType, ");
                query.AppendLine("PPM = @PPM, ");

                query.AppendLine("DeterminedBy = @DeterminedBy,");
                query.AppendLine("TestNumber = @TestNumber,");
                query.AppendLine("Impeddance = @Impeddance,");
                query.AppendLine("Taps = @Taps,");
                query.AppendLine("Polarity = @Polarity,");
                query.AppendLine("ManufDate = @ManufDate,");
                query.AppendLine("Sticker = @Sticker ");

                query.AppendLine("WHERE TransformerId = @TransformerId");

                listParameters.Add(DBHelper.CreateIntParameter("@TransformerId", ParameterDirection.Input, false, transformer.Id));

                if (transformer.Latitude != Double.MinValue)
                    listParameters.Add(DBHelper.CreateDecimalParameter("@Latitude", ParameterDirection.Input, false, 19, 6, transformer.Latitude));
                else
                    listParameters.Add(DBHelper.CreateDecimalParameter("@Latitude", ParameterDirection.Input, false, 19, 6, DBNull.Value));

                if (transformer.Longitude != Double.MinValue)
                    listParameters.Add(DBHelper.CreateDecimalParameter("@Longitude", ParameterDirection.Input, false, 19, 6, transformer.Longitude));
                else
                    listParameters.Add(DBHelper.CreateDecimalParameter("@Longitude", ParameterDirection.Input, false, 19, 6, DBNull.Value));

                listParameters.Add(DBHelper.CreateIntParameter("@PoleFk", ParameterDirection.Input, false, pole.Id));

                if (transformer.Comments != string.Empty)
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@Comments", 512, ParameterDirection.Input, false, transformer.Comments));
                else
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@Comments", 512, ParameterDirection.Input, false, DBNull.Value));
                /*
                if (transformer.Image != null)
                {
                    transformer.Image.GenerateNewName(transformer.Id.ToString());
                    SaveImage(transformer.Image, connectionString);
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@ImageFileName", 250, ParameterDirection.Input, false, transformer.Image.FileName));
                }
                else
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@ImageFileName", 250, ParameterDirection.Input, false, DBNull.Value));
                */
                if (!string.IsNullOrEmpty(transformer.SerialNumber))
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@SerialNumber", 100, ParameterDirection.Input, false, transformer.SerialNumber));
                else
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@SerialNumber", 100, ParameterDirection.Input, false, DBNull.Value));

                if (transformer.TransformerType != null && transformer.TransformerType.Id != int.MinValue && transformer.TransformerType.Id > 0)
                    listParameters.Add(DBHelper.CreateIntParameter("@TransformerType", ParameterDirection.Input, false, transformer.TransformerType.Id));
                else
                    listParameters.Add(DBHelper.CreateIntParameter("@TransformerType", ParameterDirection.Input, false, DBNull.Value));

                if (transformer.Type != null && transformer.Type.Id != int.MinValue && transformer.Type.Id > 0)
                    listParameters.Add(DBHelper.CreateIntParameter("@Type", ParameterDirection.Input, false, transformer.Type.Id));
                else
                    listParameters.Add(DBHelper.CreateIntParameter("@Type", ParameterDirection.Input, false, DBNull.Value));

                if (transformer.Class != null && transformer.Class.Id != int.MinValue && transformer.Class.Id > 0)
                    listParameters.Add(DBHelper.CreateIntParameter("@Class", ParameterDirection.Input, false, transformer.Class.Id));
                else
                    listParameters.Add(DBHelper.CreateIntParameter("@Class", ParameterDirection.Input, false, DBNull.Value));

                if (transformer.Phase != null && transformer.Phase.Id != int.MinValue && transformer.Phase.Id > 0)
                    listParameters.Add(DBHelper.CreateIntParameter("@Phase", ParameterDirection.Input, false, transformer.Phase.Id));
                else
                    listParameters.Add(DBHelper.CreateIntParameter("@Phase", ParameterDirection.Input, false, DBNull.Value));

                if (!string.IsNullOrEmpty(transformer.UtilityNumber))
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@UtilityNumber", 100, ParameterDirection.Input, false, transformer.UtilityNumber));
                else
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@UtilityNumber", 100, ParameterDirection.Input, false, DBNull.Value));

                if (!string.IsNullOrEmpty(transformer.Manufacturer))
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@Manufacturer", 100, ParameterDirection.Input, false, transformer.Manufacturer));
                else
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@Manufacturer", 100, ParameterDirection.Input, false, DBNull.Value));

                if (!string.IsNullOrEmpty(transformer.Primary))
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@Primary", 100, ParameterDirection.Input, false, transformer.Primary));
                else
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@Primary", 100, ParameterDirection.Input, false, DBNull.Value));

                if (!string.IsNullOrEmpty(transformer.Secondary))
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@Secondary", 100, ParameterDirection.Input, false, transformer.Secondary));
                else
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@Secondary", 100, ParameterDirection.Input, false, DBNull.Value));

                if (!string.IsNullOrEmpty(transformer.TransformerNumber))
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@PoleNumber", 100, ParameterDirection.Input, false, transformer.TransformerNumber));
                else
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@PoleNumber", 100, ParameterDirection.Input, false, DBNull.Value));

                listParameters.Add(DBHelper.CreateBitParameter("@Arrestor", ParameterDirection.Input, false, transformer.Arrestor));
                listParameters.Add(DBHelper.CreateBitParameter("@DualRated", ParameterDirection.Input, false, transformer.DualRated));
                listParameters.Add(DBHelper.CreateBitParameter("@Pre1978", ParameterDirection.Input, false, transformer.Pre1978));
                listParameters.Add(DBHelper.CreateBitParameter("@CutOut", ParameterDirection.Input, false, transformer.CutOut));

                if (transformer.PCBType != null && transformer.PCBType.Id != int.MinValue && transformer.PCBType.Id > 0)
                    listParameters.Add(DBHelper.CreateIntParameter("@PCBType", ParameterDirection.Input, false, transformer.PCBType.Id));
                else
                    listParameters.Add(DBHelper.CreateIntParameter("@PCBType", ParameterDirection.Input, false, DBNull.Value));

                if (transformer.ArrestorKv != null && transformer.ArrestorKv.Id != int.MinValue && transformer.ArrestorKv.Id > 0)
                    listParameters.Add(DBHelper.CreateIntParameter("@ArrestorKv", ParameterDirection.Input, false, transformer.ArrestorKv.Id));
                else
                    listParameters.Add(DBHelper.CreateIntParameter("@ArrestorKv", ParameterDirection.Input, false, DBNull.Value));

                if (transformer.CutOutKv != null && transformer.CutOutKv.Id != int.MinValue && transformer.CutOutKv.Id > 0)
                    listParameters.Add(DBHelper.CreateIntParameter("@CutOutKv", ParameterDirection.Input, false, transformer.CutOutKv.Id));
                else
                    listParameters.Add(DBHelper.CreateIntParameter("@CutOutKv", ParameterDirection.Input, false, DBNull.Value));

                if (!string.IsNullOrEmpty(transformer.PPM))
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@PPM", 100, ParameterDirection.Input, false, transformer.PPM));
                else
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@PPM", 100, ParameterDirection.Input, false, DBNull.Value));

                if (transformer.Can != null && transformer.Can.Id != int.MinValue && transformer.Can.Id > 0)
                    listParameters.Add(DBHelper.CreateIntParameter("@Can", ParameterDirection.Input, false, transformer.Can.Id));
                else
                    listParameters.Add(DBHelper.CreateIntParameter("@Can", ParameterDirection.Input, false, DBNull.Value));

                if (transformer.DeterminedBy != null && transformer.DeterminedBy.Id != int.MinValue && transformer.DeterminedBy.Id > 0)
                    listParameters.Add(DBHelper.CreateIntParameter("@DeterminedBy", ParameterDirection.Input, false, transformer.DeterminedBy.Id));
                else
                    listParameters.Add(DBHelper.CreateIntParameter("@DeterminedBy", ParameterDirection.Input, false, DBNull.Value));

                if (!string.IsNullOrEmpty(transformer.TestNumber))
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@TestNumber", 100, ParameterDirection.Input, false, transformer.TestNumber));
                else
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@TestNumber", 100, ParameterDirection.Input, false, DBNull.Value));

                if (!string.IsNullOrEmpty(transformer.Impeddance))
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@Impeddance", 100, ParameterDirection.Input, false, transformer.Impeddance));
                else
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@Impeddance", 100, ParameterDirection.Input, false, DBNull.Value));

                if (!string.IsNullOrEmpty(transformer.Taps))
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@Taps", 100, ParameterDirection.Input, false, transformer.Taps));
                else
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@Taps", 100, ParameterDirection.Input, false, DBNull.Value));

                if (!string.IsNullOrEmpty(transformer.Polarity))
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@Polarity", 100, ParameterDirection.Input, false, transformer.Polarity));
                else
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@Polarity", 100, ParameterDirection.Input, false, DBNull.Value));

                if (transformer.ManufDate != DateTime.MinValue)
                    listParameters.Add(DBHelper.CreateDateTimeParameter("@ManufDate", ParameterDirection.Input, false, transformer.ManufDate));
                else
                    listParameters.Add(DBHelper.CreateDateTimeParameter("@ManufDate", ParameterDirection.Input, false, DBNull.Value));

                listParameters.Add(DBHelper.CreateBitParameter("@Sticker", ParameterDirection.Input, false, transformer.Sticker));

                LMobileDatabase.ExecuteSingleQueryWhithParameters(query.ToString(), connectionString, listParameters.ToArray());

                #endregion

                #region Update Notes

                query = new StringBuilder("DELETE tb_Transformers_Keywords WHERE idTransformer = @idTransformer");
                listParameters.Clear();
                listParameters.Add(DBHelper.CreateIntParameter("@idTransformer", ParameterDirection.Input, false, transformer.Id));
                LMobileDatabase.ExecuteSingleQueryWhithParameters(query.ToString(), connectionString, listParameters.ToArray());

                foreach (EDistributionLookUp note in transformer.Notes)
                {
                    query = new StringBuilder("INSERT INTO tb_Transformers_Keywords (idTransformer, idKeyword) VALUES (");
                    query.AppendLine("@idTransformer, @idKeyword)");

                    listParameters.Clear();
                    listParameters.Add(DBHelper.CreateIntParameter("@idTransformer", ParameterDirection.Input, false, transformer.Id));
                    listParameters.Add(DBHelper.CreateIntParameter("@idKeyword", ParameterDirection.Input, false, note.Id));

                    LMobileDatabase.ExecuteSingleQueryWhithParameters(query.ToString(), connectionString, listParameters.ToArray());
                }
                
                #endregion

                #region - Image UPDATE -

                query = new StringBuilder("DELETE tb_TransformerImages WHERE idTransformer = @idTransformer");
                listParameters.Clear();
                listParameters.Add(DBHelper.CreateIntParameter("@idTransformer", ParameterDirection.Input, false, transformer.Id));
                LMobileDatabase.ExecuteSingleQueryWhithParameters(query.ToString(), connectionString, listParameters.ToArray());

                int imagesCount = 0;

                foreach (EDistributionPoleImage image in transformer.Images)
                {
                    query = new StringBuilder("INSERT INTO tb_TransformerImages (idTransformer, FileName) VALUES (");
                    query.AppendLine("@idTransformer, @FileName)");

                    image.GenerateNewName(transformer.Id.ToString() + imagesCount);
                    listParameters.Clear();
                    listParameters.Add(DBHelper.CreateIntParameter("@idTransformer", ParameterDirection.Input, false, transformer.Id));
                    listParameters.Add(DBHelper.CreateNVarCharParameter("@FileName", 150, ParameterDirection.Input, false, image.FileName));

                    LMobileDatabase.ExecuteSingleQueryWhithParameters(query.ToString(), connectionString, listParameters.ToArray());
                    SaveImage(image, connectionString);
                    imagesCount++;
                }

                #endregion


                return transformer;
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        public static void UpdateUploadedData(List<ETransformer> transformers, Int32 poleId, string connectionString)
        {
            foreach (ETransformer transformer in transformers)
            {
                StringBuilder _query = new StringBuilder();
                _query.AppendLine("UPDATE tb_Transformers set TransformerServerID = " + transformer.ServerId + ", PoleServerID = " + poleId + ", UploadedDate = " +
                                  DateTime.Now.ToString("yyyy-MM-dd") + " where TransformerId = " + transformer.Id + " ");

                LMobileDatabase.ExecuteQuery(_query.ToString(), connectionString);
            }
        }

        private static Int32 SaveImage(EDistributionPoleImage image, string connectionString)
        {
            Int32 returnId = image.Id;

            if (image.Id == Int32.MinValue)
            {
                string newImageName = image.FileName;
                DirHelper.CopyImageToOutbox(connectionString, image.FullName, newImageName);
            }

            return returnId;
        }

        public static void Delete(string connectionString, ETransformer transformer)
        {
            var listParameters = new List<SqlParameter>();
            var query = new StringBuilder();

            try
            {
                #region - Meter DELETE -

                query.AppendLine("DELETE tb_Transformers ");
                query.AppendLine("WHERE TransformerId = @TransformerId ");

                listParameters.Add(DBHelper.CreateIntParameter("@TransformerId", ParameterDirection.Input, false, transformer.Id));

                LMobileDatabase.ExecuteSingleQueryWhithParameters(query.ToString(), connectionString, listParameters.ToArray());

                #endregion
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }

        public static List<EDistributionLookUp> SaveTransformerNotes(List<EDistributionLookUp> notes, Int32 transformerId)
        {
            return WebConsumer.Distribution.WPole.SaveTransformerNotes(transformerId, notes);
        }
    }
}
